package fragment;

import factory.ListViewFactory;
import holder.HomeHolder;
import holder.HomePictureHolder;
import holder.ItemHolder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import manager.DownLoadManager;

import org.apache.http.client.protocol.RequestTargetAuthentication;

import protocol.HomeProtocol;

import com.example.googlemarketdemo.R;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseStream;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import conf.constants.URLS;

import base.BaseFragment;
import base.BaseHolder;
import base.BaseItemAdapter;
import base.SuperBaseAdapter;
import base.LoadingPager.LoadResult;

import bean.AppInfoBean;
import bean.HomeBean;
import utils.UIUtils;

import android.graphics.Color;

import android.os.SystemClock;

import android.view.View;

import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

public class HomeFragment extends BaseFragment {

	private List<String> mDatas;
	private List<AppInfoBean> mAppInfoBeans;
	private List<String> mPicture;
	private HomeProtocol homeProtocol;
	private HomeAdapter madapter;

	/**
	 * 
	 */
	@Override
	protected LoadResult initData() {
		/*
		 * SystemClock.sleep(2000); // 成功 // 失败 // 错误 // public static final int
		 * STATE_LOADING = 0;//加载页面 // public static final int STATE_EMPTY =
		 * 1;//空页面 // public static final int STATE_ERROR = 2;//错误页面 //测试数据
		 * //使用xUtils发送网络请求 try { HttpUtils httpUtils = new HttpUtils(); String
		 * url = URLS.BaseUrl +"home";
		 * 
		 * //请求的参数 RequestParams params = new RequestParams();
		 * params.addQueryStringParameter("index", "0"); ResponseStream
		 * responseStream = httpUtils.sendSync(HttpMethod.GET, url, params);
		 * 
		 * String jsonString = responseStream.readString();
		 * System.out.println("HomeFragment: "+jsonString); //解析json数据 Gson gson
		 * = new Gson(); HomeBean homeBean = gson.fromJson(jsonString,
		 * HomeBean.class);
		 */

		/*-----------  begin ----------*/

		homeProtocol = new HomeProtocol();
		try {
			HomeBean homeBean = homeProtocol.loadData(0);

			LoadResult state = checkState(homeBean);
			if (state != LoadResult.SUCCESS) {
				return state;
			}

			state = checkState(homeBean.list);

			if (state != LoadResult.SUCCESS) {
				return state;
			}

			// 赋值
			mAppInfoBeans = homeBean.list;
			mPicture = homeBean.picture;
			return state;

		} catch (Exception e) {
			e.printStackTrace();
			return LoadResult.ERROR;
		}
	}

	@Override
	protected View initSuccessView() {
		ListView listView = new ListViewFactory().createListView();
		/*-----------  添加轮播图 ----------*/

		HomePictureHolder homePictureHolder = new HomePictureHolder();
		listView.addHeaderView(homePictureHolder.mHolderView);
		homePictureHolder.setDataAndRefreshHolderView(mPicture);

		madapter = new HomeAdapter(listView, mAppInfoBeans);
		listView.setAdapter(madapter);

		return listView;
	}

	class HomeAdapter extends BaseItemAdapter {

		public HomeAdapter(AbsListView ablistView, List<AppInfoBean> mDatas) {
			super(ablistView, mDatas);
			// TODO Auto-generated constructor stub
		}

		/*
		 * @Override protected BaseHolder<AppInfoBean> getSpecialHolder(int
		 * position) { // TODO Auto-generated method stub return new
		 * HomeHolder(); }
		 */

		/**
		 * 加载更多的方法
		 */
		@Override
		protected List<AppInfoBean> onLoadMore() throws Exception {
			SystemClock.sleep(2000);
			return perfromLoadMore();
		}

	}

	/**
	 * 加载更多的方法
	 * 
	 * @return
	 * @throws HttpException
	 * @throws IOException
	 * @throws Exception
	 */
	private List<AppInfoBean> perfromLoadMore() throws HttpException,
			IOException, Exception {

		HomeBean homeBean = homeProtocol.loadData(mAppInfoBeans.size());
		if (homeBean != null) {
			return homeBean.list;
		}
		return null;
	}

	@Override
	public void onPause() {
		// 移除观察者
		List<ItemHolder> getmItemHolders = madapter.getmItemHolders();
		for (int i = 0; i < getmItemHolders.size(); i++) {

			DownLoadManager.getInstance()
					.deleteObserver(getmItemHolders.get(i));
		}
		super.onPause();
	}

	@Override
	public void onResume() {
		if (madapter != null) {
			// 添加观察者
			List<ItemHolder> mItemHolders = madapter.getmItemHolders();
				for (int i = 0; i < mItemHolders.size(); i++) {
					DownLoadManager.getInstance()
					.addObserver(mItemHolders.get(i));
				}
				
			madapter.notifyDataSetChanged();
		}
		
		super.onResume();
	}

}
